package sms

import (
	"fmt"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
	"math/rand"
	"time"
	"yundian/internel/pkg/dingo/global"
)

// SmsAliParams 发送参数
func SmsAliParams(phone string) (*dysmsapi.SendSmsResponse, string, error) {
	code := fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
	if code[0:1] == "0" {
		code = "8" + code[1:]
	}

	client, err := dysmsapi.NewClientWithAccessKey(global.Cfg.Ali.AccessRegionid, global.Cfg.Ali.AccessKey, global.Cfg.Ali.AccessKeySecret)
	if err != nil {
		return nil, "", err
	}

	req := dysmsapi.CreateSendSmsRequest()
	req.Method = "POST"
	req.Scheme = "https"
	req.QueryParams["PhoneNumbers"] = phone
	req.QueryParams["SignName"] = "中曼物联"
	req.QueryParams["TemplateCode"] = "SMS_218615274"
	req.QueryParams["TemplateParam"] = "{\"code\":" + code + "}"

	response, err := client.SendSms(req)
	if err != nil || response.Code != "OK" || response.Message != "OK" {
		return response, code, err
	}

	return response, code, err
}
